﻿using System;
using System.Collections.Generic;
using System.IO;
using Module.Core.LoginSystem.Commons;
using Module.Core.LoginSystem.Commons.Framwork;
using Module.Core.LoginSystem.Commons.Utilities;
using Module.Core.LoginSystem.Components.Audits;
using Module.Core.LoginSystem.Components.Commands;
using Module.Core.LoginSystem.Components.Geographical;
using Module.Core.LoginSystem.Components.Modules;
using Module.Core.LoginSystem.Components.Roles;
using Module.Core.LoginSystem.Components.Users;
namespace Web.Cms.Users
{
    public partial class UserProfile : AdminUserControl
    {
        private int _userId;
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            if (!Directory.Exists(Server.MapPath("/Modules/Uploads/UserAvatar/" + CurrentUser.Username)))
            {
                Directory.CreateDirectory(Server.MapPath("/Modules/Uploads/UserAvatar/" + CurrentUser.Username));
            }
            txtAvatar.fpUploadDir = "/Modules/Uploads/UserAvatar/" + CurrentUser.Username;
        }
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            CommandInfo _commandInfo = CommandController.GetCommand("admin_usermanager");
            RolePermisionInfo _roleInfo = RoleController.GetReadWritePermison(CurrentUser.UserID, _commandInfo.CommandID);
            if (_roleInfo == null) return;
            if (_roleInfo.Write == 0)
            {
                btnUpdate.Visible = false;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {

            _userId = ConvertUtility.ToInt32(Request.QueryString["userid"]);
            if (!Page.IsPostBack)
            {
                UserInfo _user = UserController.GetUser(_userId);
                if (_user == null) return;
                lblUserId.Text = _userId.ToString();
                txtAdditionalInfo.Text = _user.AdditionalInfo;
                txtAddress.Text = _user.Address;
                txtCmtnd.Text = _user.Cmtnd;
                txtEmail.Text = _user.Email;
                txtFirstName.Text = _user.FirstName;
                txtLastName.Text = _user.LastName;
                txtMobile.Text = _user.Mobile;
                txtSkype.Text = _user.Skype;
                txtYm.Text = _user.Ym;
                txtTel.Text = _user.Tel;
                txtUsername.Text = _user.Username;
                txtAvatar.Text = _user.Avatar;
                LoadCityInfo();
                if (_user.Gender)
                {
                    rdoGender.SelectedValue = "0";
                }
                else
                    rdoGender.SelectedValue = "1";
                radBirth.SelectedDate = ConvertUtility.ToDateTime(_user.Birth);
                if (_user.Avatar == string.Empty)
                {

                    imgAvartar.ImageUrl = "~/App_Themes/Default/Images/NO_PHOTO_AVAILABLE.jpg";

                }
                else
                {
                    imgAvartar.ImageUrl = MultimediaUtilities.GetAvatar(_user.Avatar);
                }
            }
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect(
                        UIProcess.GetCPPage(CurrentPortalID, "admin_usermanager", new string[] { },
                                            new string[] { "" }));
        }
        public void LoadCityInfo()
        {
            //List<CityInfo> _lstCities = GeographicalController.GetCities(1);
            //drCity.DataTextField = "Name";
            //drCity.DataValueField = "CityID";
            //drCity.DataSource = _lstCities;
            //drCity.DataBind();
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {

            try
            {
                UserInfo _userInfo = UserController.GetUser(ConvertUtility.ToInt32(lblUserId.Text));
                _userInfo.Username = txtUsername.Text.Trim();
                _userInfo.FirstName = txtFirstName.Text.Trim();
                _userInfo.LastName = txtLastName.Text.Trim();
                _userInfo.Mobile = txtMobile.Text.Trim();
                _userInfo.Tel = txtTel.Text.Trim();
                _userInfo.Address = txtAddress.Text.Trim();
                _userInfo.Cmtnd = txtCmtnd.Text.Trim();
                _userInfo.Email = txtEmail.Text.Trim();
                _userInfo.AdditionalInfo = txtAdditionalInfo.Text.Trim();
                _userInfo.Ym = txtYm.Text.Trim();
                _userInfo.Skype = txtSkype.Text.Trim();
                _userInfo.Avatar = txtAvatar.Text.Trim();
                MultimediaUtilities.SetAvatarThumbnail(Server.MapPath(_userInfo.Avatar), 120, 0);
                _userInfo.Birth = radBirth.SelectedDate;
                if (rdoGender.SelectedValue == "0")
                {
                    _userInfo.Gender = true;
                }
                else
                {
                    _userInfo.Gender = false;
                }
                _userInfo.City = ConvertUtility.ToInt32(drCity.SelectedValue);
                UserController.UpdateUser(_userInfo);
                // Insert Cores_Audits
                ModuleInfo _moduleInfo = ModuleController.GetModule(CurrentCommand.CtrlKey);
                AuditController.CreateAuditQ(_moduleInfo.ModuleDefID, (int)AuditCode.Update, CurrentCommand.CtrlKey, CurrentUser.UserID, ConvertUtility.ToInt32(lblUserId.Text)
                                            , _userInfo.Username, 1, DateTime.Now);

                Response.Redirect(
                        UIProcess.GetCPPage(CurrentPortalID, "admin_usermanager", new string[] { },
                                            new string[] { "" }));
            }
            catch (Exception ex)
            {
                lblUpdateStatus.Text = ex.Message;
            }
        }
    }
}